﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections.Specialized;


public partial class Admin_MaintainAccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["typeofAccount"] == null)
            {
                Session["typeofAccount"] = "Student";
                
            }
            loadData(getTypeUser());        
            if (Request["Insert"] != null)
            {
                Control ct;
                ct = Page.LoadControl("~/Share/Insert_UpdatePersonControl.ascx");
                pn_PersonDetails.Controls.Add(ct);
            }            
        }
        if (Request["UserID"] != null)
        {
            Control ct;
            ct = Page.LoadControl("~/Share/PersonDetails.ascx");
            pn_PersonDetails.Controls.Add(ct);
        }
    }
    public IList<UserInfo> getTypeUser()
    {
        string typeuser=Session["typeofAccount"].ToString();
        IList<UserInfo> list = new List<UserInfo>();
        if (typeuser.Equals("Staff"))
            list = userDAO.GetAllStaff();
        else  list = userDAO.GetUserByType(typeuser);
        return list;
    }
    public void loadData(IList<UserInfo> listuser)
    {
        string typeofPerson = Session["typeofAccount"].ToString();
        lb_Search.Text = typeofPerson;

        lb_list.Text = Session["typeofAccount"].ToString() + "'s List";        
        btn_Insert.Text = "Add new " + Session["typeofAccount"].ToString();              
        grv_Account.DataSource = listuser;            
        grv_Account.DataBind();
        if (typeofPerson.Equals("Student"))
        {
            grv_Account.Columns[6].Visible = false;
        }
        else if (typeofPerson.Equals("Staff"))
        {
            grv_Account.Columns[5].Visible = false;
            foreach (GridViewRow row in grv_Account.Rows)
            {
                if (((Label)row.FindControl("lb_UserID")).Text.Equals("Admin"))
                {
                    ((Label)row.FindControl("lb_type")).Text = "Admin";
                }
                else if (((Label)row.FindControl("lb_UserID")).Text.StartsWith("Stf"))
                {
                    ((Label)row.FindControl("lb_type")).Text = "Staff";
                }
                else if (((Label)row.FindControl("lb_UserID")).Text.StartsWith("Mng"))
                {
                    ((Label)row.FindControl("lb_type")).Text = "Manager";
                }
            }

        }
        else
        {
            grv_Account.Columns[4].Visible = false;
            grv_Account.Columns[5].Visible = false;
            grv_Account.Columns[6].Visible = false;

        }
    }

    protected void lbtn_Student_Account_Click(object sender, EventArgs e)
    {
        Session["typeofAccount"] = "Student";
        
        Response.Redirect("~/Admin/MaintainAccount.aspx");
    }
    protected void lbtn_Staff_Account_Click(object sender, EventArgs e)
    {        
        Session["typeofAccount"] = "Staff";
        
        Response.Redirect("~/Admin/MaintainAccount.aspx");
    }
    protected void ltbn_Customer_Account_Click(object sender, EventArgs e)
    { 
        Session["typeofAccount"] = "Customer";
        Response.Redirect("~/Admin/MaintainAccount.aspx");
    }
    
    
    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/InsertPerson.aspx?typeofAccount=" + Session["typeofAccount"].ToString());

    }
    UserDAO userDAO = new UserDAO();
    UserInfo user = new UserInfo();

    protected void btn_Delele_Click(object sender, EventArgs e)
    {
        string id = "";
        try
        {
            bool ischeck = false;

            foreach (GridViewRow row in grv_Account.Rows)
            {
                if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                {
                    // ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the exhibition to delete!');", true);
                    ischeck = true;
                }

            }

            if (ischeck == true)
            {
                foreach (GridViewRow row in grv_Account.Rows)
                {

                  
                    if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                    {
                        id = grv_Account.DataKeys[row.RowIndex].Value.ToString();
                        if (userDAO.Delete(id) > 0)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete successfull!');", true);
                        }
                        else
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Failed!');", true);
                    }

                }
                loadData(getTypeUser());
            }
            else { ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose a Account to delete!');", true); }
        }
        catch (Exception ex) { }



        
    }

    protected void grv_Account_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    public void onCheckedChange(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in grv_Account.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in grv_Account.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = false;
            }
        }
    }

 


    protected void grv_Account_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.Header) //check for RowType
        //{
        //    Button b = (Button)e.Row.FindControl("btn_Delete"); //access the LinkButton from the Header TemplateField using FindControl
        //    b.Attributes.Add("onclick", "return ConfirmOnDelete();"); //attach the JavaScript function
        //}

    }
    protected void grv_Account_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }


    protected void grd_Account_SelectedIndexChange(object sender, GridViewSelectEventArgs e)
    {
      //  onCheckedChange();
    }
    protected void grv_Account_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Account.PageIndex = e.NewPageIndex;
        loadData(getTypeUser());       
    }
    protected void btn_BlockAccount_Click(object sender, EventArgs e)
    {
        AccountDAO accDAO = new AccountDAO();
         string id = "";
        foreach (GridViewRow row in grv_Account.Rows)
        {
          
            if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
            { 
                id = grv_Account.DataKeys[row.RowIndex].Value.ToString();
                if (id.Equals("Admin"))
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Cannot block Admin');", true);
                }
                else
                {
                    accDAO.BlockAccount(id);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('This account(s) blocked');", true);
                }

            }          
        }
        loadData(getTypeUser());
    }

    protected void btnUnBlock_Click(object sender, EventArgs e)
    {
        AccountDAO accDAO = new AccountDAO();
        string id = "";
        foreach (GridViewRow row in grv_Account.Rows)
        {

            if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
            {
                id = grv_Account.DataKeys[row.RowIndex].Value.ToString();
                int roleID = 6;
                if(id.StartsWith("Stf"))
                    roleID=2;
                else if (id.StartsWith("Mng"))
                    roleID=3;
                else if (id.StartsWith("Std"))
                    roleID = 4;
                else if (id.StartsWith("Cus"))
                    roleID = 5;
                accDAO.UnBlockAccount(id,roleID);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('This account(s) blocked');", true);

            }
        }
        loadData(getTypeUser());
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string userSeaech = txtSearch.Text;
        string typeperson_search = Session["typeofAccount"].ToString();
        if(combx_Search.SelectedValue.ToString().Equals("UserID"))                       
            loadData(userDAO.SearchUser_ByID(userSeaech, typeperson_search));
         else if(combx_Search.SelectedValue.ToString().Equals("Name"))
        loadData(userDAO.SearchUser_ByName("%"+userSeaech, typeperson_search));
        lb_list.Text = "Search Result " + typeperson_search + "'s List";
    }
}
